package com.yiren.dbaa.module.common.view.activity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;

import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;
import com.yiren.dbaa.R;
import com.yiren.dbaa.activity.BaseActivity;
import com.yiren.dbaa.module.device.contract.DeviceConnectContract;
import com.yiren.dbaa.module.device.presenter.DeviceConnectPresenter;
import com.yiren.dbaa.view.toast.MessageBox;

import java.util.List;

/**
 * Describe 启动加载页面
 *
 * @author WRX
 * @date 2022/10/18
 */
public class SplashActivity extends BaseActivity implements DeviceConnectContract.View {

    private final String TAG = SplashActivity.class.getSimpleName();

    private ImageView ivStart;
    private DeviceConnectContract.Presenter deviceConnectPresenter;

    private String[] permissions = new String[]{
            Permission.MANAGE_EXTERNAL_STORAGE,
            Permission.READ_PHONE_STATE,
            Permission.ACCESS_COARSE_LOCATION,
//            Permission.WRITE_SETTINGS
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_splash);
        ivStart = (ImageView) findViewById(R.id.iv_start);
        deviceConnectPresenter = new DeviceConnectPresenter(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        requestPermissions();
    }

    /**
     * 获取动态请求权限
     */
    private void requestPermissions() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            XXPermissions.with(this)
                    // 不适配 Android 11 可以这样写
                    //.permission(Permission.Group.STORAGE)
                    // 适配 Android 11 需要这样写，这里无需再写 Permission.Group.STORAGE
                    .permission(permissions)
                    .request(new OnPermissionCallback() {

                        @Override
                        public void onGranted(List<String> permissions, boolean all) {
                            if (all) {
                                Log.d(TAG, "获取权限成功");
                                startAnimation();
                            }
                        }

                        @Override
                        public void onDenied(List<String> permissions, boolean never) {
                            if (never) {
                                MessageBox.show(SplashActivity.this, "被永久拒绝授权，请手动授予存储权限");
                                // 如果是被永久拒绝就跳转到应用权限系统设置页面
                                XXPermissions.startPermissionActivity(SplashActivity.this, permissions);
                            } else {
                                MessageBox.show(SplashActivity.this, "获取权限失败");
                                startAnimation();
                            }
                        }
                    });
        } else {
            startAnimation();
        }
    }

    private void startAnimation() {
        ScaleAnimation scaleAnimation = new ScaleAnimation(0.2f, 0.8f, 0.2f, 0.8f, 1, 0.5F, 1, 0.5F);

        AlphaAnimation alphaAnimation = new AlphaAnimation(0.3f, 1.0f);

        AnimationSet animationSet = new AnimationSet(true);
        animationSet.setFillAfter(true);
        animationSet.setDuration(3000);
        animationSet.setStartTime(200);
        animationSet.addAnimation(scaleAnimation);
        animationSet.addAnimation(alphaAnimation);
        ivStart.setImageResource(R.mipmap.startbg);
        ivStart.startAnimation(animationSet);

        animationSet.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                deviceConnectPresenter.connectDevice(SplashActivity.this);
                Intent intent = new Intent(SplashActivity.this, MainActivity.class);
                startActivity(intent);
                SplashActivity.this.finish();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }

    @Override
    public void deviceConnectSuccess() {

    }
}
